package com.smalls.newvideotwo.mvp.presenter;

import com.smalls.newvideotwo.mvp.base.BaseBeanResult;
import com.smalls.newvideotwo.mvp.base.BasePresenter;
import com.smalls.newvideotwo.mvp.bean.ApkInfoItem;
import com.smalls.newvideotwo.mvp.http.constants.PortalErr;
import com.smalls.newvideotwo.mvp.http.constants.PortalErrUtil;
import com.smalls.newvideotwo.mvp.model.GetApkInfoModel;
import com.smalls.newvideotwo.mvp.view.IGetApkInfoView;
import com.smalls.newvideotwo.util.LogUtil;
import com.smalls.newvideotwo.utils.Constant;

import org.xutils.ex.HttpException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.concurrent.TimeoutException;

/**
 * Created by cindy on 2018/4/15.
 */

public class GetApkInfoPresenter extends BasePresenter<IGetApkInfoView> implements BaseBeanResult.IGetApkInfo {
    private static String TAG = Constant.baddcoolio2tag? Constant.TAGPREFIXSTR + "GetApkInfoPresenter":"GetApkInfoPresenter";
    private boolean isLive;
    private int categoryLevel = 1;
    private com.smalls.newvideotwo.mvp.model.modelInterface.IGetApkInfo model = new GetApkInfoModel();

    public GetApkInfoPresenter(IGetApkInfoView view) {
        super(view);
    }

    public void requestData(boolean isLive) {
        LogUtil.d(this.TAG, "requestData");
        this.isLive = isLive;
        model.getApkInfo(this);
    }

    public void Abort() {
        if(model!=null) {
            model.Abort();
        }
    }

    @Override
    public void onError(Throwable e) {
        e.printStackTrace();
        String errCode = "";
        if ((e instanceof TimeoutException) || (e instanceof ConnectException) || (e instanceof SocketTimeoutException)) {
            LogUtil.e(this.TAG, "\u663e\u793aDialog\u4fe1\u606fEB2");
            errCode = PortalErr.STR_BOX_EPG_TIMEOUT_ERROR;
        } else {
            LogUtil.e(this.TAG, "\u663e\u793aDialog\u4fe1\u606fEB3");
            errCode = PortalErr.PORTAL_SYSTEM;
        }
        PortalErrUtil.getInstance().setErrCode(errCode);
        ((IGetApkInfoView) this.view).showErrDialogGetApkInfo(errCode, 1);
        if (e instanceof HttpException) {

        }
    }

    @Override
    public void onNext(ApkInfoItem result) {
        PortalErrUtil.getInstance().setErrCode("");
        if (result == null ) {
            LogUtil.e(this.TAG, "\u663e\u793aDialog\u4fe1\u606fEB1");
            PortalErrUtil.getInstance().setErrCode(PortalErr.PORTAL_ROOT_COLUMN);
            ((IGetApkInfoView) this.view).showErrDialogGetApkInfo(PortalErr.PORTAL_ROOT_COLUMN, 1);
        } else {
            if (this.isLive ) {
                LogUtil.e(this.TAG, "\u663e\u793aDialog\u4fe1\u606fED1");
                PortalErrUtil.getInstance().setErrCode(PortalErr.STR_PRODUCT_WITHOUT_AUTH);
                ((IGetApkInfoView) this.view).showErrDialogGetApkInfo(PortalErr.STR_PRODUCT_WITHOUT_AUTH, this.categoryLevel);
            } else {
                ((IGetApkInfoView) this.view).onSucceedGetApkInfo(result, this.categoryLevel);
            }
        }
    }
}
